﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CreditCard
{
    public partial class UserHome : Form
    {
        public UserHome()
        {
            InitializeComponent();
        }

        public UserHome(AppHome app, User user)
        {
            InitializeComponent();
            this.user = user;
            this.app = app;
            getMsg();
        }

        User user;

        AppHome app;

        MySqlDao sqlDao = new MySqlDao();


        List<BaseCard> list;


        bool e3 = true;
        private void getMsg()
        {
            DataSet ds = sqlDao.selectMsg(user.U_id, 0);
            if (ds.Tables[0].Rows.Count > 0)
            {
                dataGridView3.DataSource = ds.Tables[0].DefaultView;
                if (e3)
                {
                    DataGridViewButtonColumn column = new DataGridViewButtonColumn();
                    column.Text = "已 读";
                    column.Name = "编 辑";
                    column.UseColumnTextForButtonValue = true;
                    dataGridView3.Columns.Add(column);
                    dataGridView3.Columns["内容"].Width = 640;
                    e3 = false;
                }
            }
        }

        private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.ColumnIndex == 0 && MessageBox.Show("下次不在显示该信息！", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string m_id = dataGridView3.Rows[e.RowIndex].Cells["信息ID"].Value.ToString();
                if (sqlDao.updateMsgState(m_id, -1))
                {
                    MessageBox.Show("下次不在显示该条信息...");
                }
            }
        }

        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPageIndex == 0)
            {
                getMsg();
            }
            else if(e.TabPageIndex == 1) 
            { 
                //信用卡申请
                list = sqlDao.getBase();
                term.Text = list[0].B_term.ToString();
                limit.Text = list[0].B_limit.ToString();
                type.DataSource = list;
                type.DisplayMember = "b_type";
                type.ValueMember = "b_term";
            }
            else if (e.TabPageIndex == 3)
            {
                //信用卡信息
                DataSet ds = sqlDao.selectCard(user.U_id, 3);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView1.DataSource = ds.Tables[0].DefaultView;
                }
            }
            else if (e.TabPageIndex == 4)
            {
                //交易信息
                DataSet ds = sqlDao.selectTransfer(user.U_id);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView2.DataSource = ds.Tables[0].DefaultView;
                }
            }
            else if(e.TabPageIndex==5)
            {
                //个人信息
                u_id.Text = user.U_id.ToString();
                u_tel.Text = user.U_tel.ToString();
                u_name.Text = user.U_name.ToString();
            }
            else if((e.TabPageIndex == 6))
            {
                if(MessageBox.Show("确定要退出？","警告",MessageBoxButtons.YesNo)==DialogResult.Yes)
                    app.openForm(new Home(app));
            }
        }

        private void submit_Click(object sender, EventArgs e)
        {
            if (type.Text == "" || pass.Text == ""  || npass.Text == "")
            {
                MessageBox.Show("请将信息填写完整");
            }
            else if (pass.Text != npass.Text)
            {
                MessageBox.Show("两次密码不一致...");
            }
            else
            {
                Card card = new Card();
                card.C_pass = pass.Text;
                card.C_id = "6889"+Utils.getTimeTicks();
                card.U_id = user.U_id;
                
                foreach (var item in list)
                {
                    if (item.B_type == type.Text)
                    {
                        card.B_id = item.B_id;
                        card.C_time = DateTime.Now.AddYears(item.B_term).ToString("yyyy-MM-dd");
                    }
                }
                if (sqlDao.insertCard(card))
                {
                    MessageBox.Show("申请已提交...");
                }
                else
                {
                    MessageBox.Show("申请失败...");
                }
            }
        }


        bool f = true;
        //信用卡类型变化
        private void type_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (f)
            {
                f = false;
                return;
            }
            term.Text = type.SelectedValue.ToString();
            foreach (var item in list)
            {
                if (item.B_term == Convert.ToInt32(term.Text))
                {
                    limit.Text = item.B_limit.ToString();
                }
            }
        }


        //激活卡
        private void activation_Click(object sender, EventArgs e)
        {
            if (c_id.Text == "" || c_pass.Text == "")
            {
                MessageBox.Show("卡号和密码不能为空...");
            }
            else
            {
                //c_state = 2为请求激活
                if (sqlDao.updateCardState(c_id.Text, 2))
                {
                    MessageBox.Show("申请已提交...");
                }
                else
                {
                    MessageBox.Show("申请失败...");
                }
            }
        }

        //个人信息修改
        private void update_Click(object sender, EventArgs e)
        {
            if (u_name.Text == "" || u_tel.Text == "")
            {
                MessageBox.Show("号码和姓名不能为空...");
            }
            else
            {
                if (MessageBox.Show("确定要修改？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    user.U_tel = u_tel.Text;
                    user.U_name = u_name.Text;
                    if (sqlDao.updateUser(user))
                    {
                        MessageBox.Show("修改成功...");
                    }
                    else
                    {
                        MessageBox.Show("修改失败...");
                    }
                }
            }
        }

        
    }
}
